package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.page.FindPage;


import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author TanLin
 * @date 2019/11/12 20:41
 * @className com.itheima.service.impl
 * @projectName health_parent
=======
import java.util.List;

/**
 * @author Diamond
 * @time 2019/11/13/21:50
>>>>>>> 584bfb33a2669a9bf11870b801c70d7bad97eb15
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionNewDao;
    @Autowired
    private PermissionDao permissionDao;
    /**
     * 分页查询
     * @return
     * @param queryPageBean
     */
    @Override
    public PageResult findPagePermission(QueryPageBean queryPageBean) {
        String queryString = FindPage.findPage(queryPageBean);
        Integer pageSize = queryPageBean.getPageSize();
        Integer currentPage = queryPageBean.getCurrentPage();
        PageHelper.startPage(currentPage,pageSize);
        Page<Permission> checkItemPage = permissionNewDao.findPagePermission(queryString);
        return new PageResult(checkItemPage.getTotal(),checkItemPage.getResult());
    }

    /**
     * 添加权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionNewDao.add(permission);
    }

    /**
     * 编辑回显
     * @param id
     * @return
     */
    @Override
    public Permission findById(Integer id) {
        return permissionNewDao.findById(id);
    }

    /**
     * 编辑提交
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionNewDao.edit(permission);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id) {
        Long count = permissionNewDao.findbyIdRole_permission(id);
        if (count > 0) {
            throw new RuntimeException(MessageConstant.PERMISSION_ASSOCIATION);
        }
        permissionNewDao.delete(id);
    }


    @Override
    public List<Permission> findAll() {

        return permissionDao.findAll();

    }
}
